/*
********************************************************************************
* Copyright (C) 2021, xiang.D <dx_65535@163.com>.
* All right reserved.
*
* File Name   : singleton.cc
* Author      :
* Version     : V1.0
* Description :
* Journal     : 2021-10-19 init v1.0
* Brief       : Blog: https://blog.csdn.net/qq_38750572?spm=1001.2014.3001.5343
* Others      :
                Compile command: g++ -Wl,--no-as-needed -std=c++11 -pthread singleton.cc
********************************************************************************
*/
#include <iostream>
#include <unistd.h>
#include <thread>

using namespace std;

class CSingleton
{
public:

    ~CSingleton() {}

    static CSingleton *getInstance(string value);

    string getValue()
    {
        return mValue;
    }

private:

    CSingleton(const string value): mValue(value)
    {
    }

    string mValue;
};

CSingleton* CSingleton::getInstance(string value)
{
    static CSingleton mObj(value);
    return &mObj;
}

void threadFirst()
{
    CSingleton* pSingleton = CSingleton::getInstance("first");
    cout << pSingleton->getValue() << "\n";
}

void threadSecond()
{
    CSingleton* pSingleton = CSingleton::getInstance("second");
    cout << pSingleton->getValue() << "\n";
}

void threadThird()
{
    CSingleton* pSingleton = CSingleton::getInstance("third");
    cout << pSingleton->getValue() << "\n";
}

int main(int argc, char *argv[])
{
    thread t1(threadFirst);
    thread t2(threadSecond);
    thread t3(threadThird);
    t1.join();
    t2.join();
    t3.join();

    return 0;
}

